package com.aaa.system.service.impl;

import com.aaa.entity.Permission;
import com.aaa.system.dao.PermissionDao;
import com.aaa.system.service.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @program: aaa-education-parent
 * @description:
 * @author: 闫克起2
 * @create: 2024-03-06 11:36
 **/
@Service
public class PermissionServiceImpl  implements PermissionService {
    @Autowired
    private PermissionDao permissionDao;
    @Override
    public List<Permission> queryByUserId(String userId) {
        return permissionDao.selectByUserid(userId);
    }

    @Override
    public List<Permission> queryByUsername(String username) {
        List<Permission> permissions = permissionDao.selectByUsername(username);
        //查询的结果没有分层
        //存放一级菜单
        List<Permission> first=permissions.stream().filter(item->item.getPid().equals("1")).collect(Collectors.toList());

        //查询一级菜单的子菜单。---递归
        for(Permission f:first){
            List<Permission> children = getChildren(f.getId(), permissions);
            f.setChildren(children);
        }
        return first;
    }

    private List<Permission> getChildren(String pid,List<Permission> permissions){
          //子菜单对象
          List<Permission> children=new ArrayList<>();
          for(Permission p:permissions){
                if(p.getPid().equals(pid)){
                    children.add(p);
                }
          }
              //遍历二级菜单
            for(Permission c:children){
                List<Permission> children1 = getChildren(c.getId(), permissions);
                c.setChildren(children1);
            }
            return children;
    }
}









